package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    //提供一个方法处理查询所有部门的请求,响应HTML
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    //要贴上注解,因为页面要取值的话不贴@ModelAttribute去到的值是${queryObject.xxx},首字符会小写
    public String list(Model model ,@ModelAttribute("qo") QueryObject qo){//创建qo对象
        //这里不再是调用listAll的方法,而是我嗯创建的query方法
       // List<Role> roles = roleService.listAll();
        PageInfo result = roleService.query(qo);//这里没有qo对象需要我们在上面创建,返回的方法是pageResult
        model.addAttribute("result",result);
        return "role/list";// /WEB-INF/views/role/potentialList.ftl
    }
    @RequestMapping("/input")
    public String input(Long id,Model model){
        //判断id是否为空
        if (id != null) {
            Role role = roleService.get(id);
            model.addAttribute("role",role);
        }
        model.addAttribute("permission",permissionService.listAll());
        //model.addAttribute("role_permission",employeeService.listAll());
        return "role/input";// /WEB-INF/views/role/input.ftl
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids) {
        if (role.getId() == null) {
            roleService.save(role,ids);
        }else{
            roleService.update(role,ids);
        }
        //重定向跳转到list页面
        return "redirect:/role/list.do";//http://localhost/role/list.do
    }
    @RequestMapping("/delete")
    public String delete(Long id){
        //判断是否为空
        if (id != null) {
            roleService.delete(id);
        }
        //重定向,// http://localhost/role/list.do
        return "redirect:/role/list.do";
    }
}
